﻿using System;
using System.Collections.Generic;
using OigSync.Domain.Api;
using OigSync.Infrastructure.Api;
using OigSync.Model;
using Quartz;

namespace OigSync.Domain.Service.Job
{
    public class GetOrderJob : IInterruptableJob
    {
        private Boolean isInterrupt;

        private DateTime timeFrom;

        public DateTime TimeFrom
        {
            get { return timeFrom; }
            set { timeFrom = value; }
        }

        private DateTime timeTo;

        public DateTime TimeTo
        {
            get { return timeTo; }
            set { timeTo = value; }
        }

        private int startPageNumber;

        public int StartPageNumber
        {
            get { return startPageNumber; }
            set { startPageNumber = value; }
        }

        /// <summary>
        /// Constructor
        /// </summary>
        /// <param name="info">information for create GetOrder job</param>
        public GetOrderJob(GetOrderJobDetailInfo info)
        {
            timeFrom = info.TimeFrom;
            timeTo = info.TimeTo;
            startPageNumber = info.StartPageNumber;

            isInterrupt = false;
        }

        public void Execute(IJobExecutionContext context)
        {
            int totalNumberOfEntries = 0;
            int pageNumber = startPageNumber;

            do
            {
                // 线程是否退出
                if (isInterrupt)
                    break;

                IApiSetting setting = new GetOrdersApiSetting(timeFrom, timeTo, pageNumber);
                ApiBase api = new GetOrdersApi("");
                api.Setting = setting;

                try
                {
                    api.Execute();
                }
                catch (System.Exception ex)
                {
                    if (ex is ApiSettingTypeErrorException)
                    {
                        //logger.Error(String.Format("{0}: 订单下载出错...", DateTime.Now));
                        //logger.Error(String.Format("{0}: 出错原因: {1}...", DateTime.Now, ex.Message));
                        break;
                    }
                    if (ex is System.Net.WebException)
                        continue;
                }

                GetOrderApiResult result = api.Result as GetOrderApiResult;

                if (!result.HasMoreOrders)
                {
                    //logger.Info(String.Format("{0}: 线程{1}下载完成...", DateTime.Now));
                    break;
                }

                if (pageNumber > 1 && totalNumberOfEntries != result.TotalNumberOfEntries)
                {
                    pageNumber--;
                    //logger.Info(String.Format("{0}: 订单总数变化,重下第{1}页...", DateTime.Now, info.PageNumber));
                    continue;
                }

                //logger.Info(String.Format("{0}: 第{1}页下载完成...", DateTime.Now, info.PageNumber));
                WriteToServer(result.OrderList);

                totalNumberOfEntries = result.TotalNumberOfEntries;
                pageNumber++;
            } while (true);
        }

        private void WriteToServer(IList<Order> orderList)
        {

        }

        public void Interrupt()
        {
            isInterrupt = true;
        }
    }
}